package com.caseProcess.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.*;
import com.caseProcess.service.CaseService;
import com.caseProcess.service.PageService;
import com.caseProcess.utils.TimeUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/case")
public class CaseController {

    @Resource
    private CaseService caseService;

    @Resource
    private PageService pageService;

    /**
     * 警情导入
     *
     * @param jsonArrayCase 警情数据
     * @return 操作结果
     */
    @RequestMapping("/caseImport")
    public Result caseImport(String jsonArrayCase) {
        Result result = new Result();
        try {
            List<Case> caseList = JSONArray.parseArray(jsonArrayCase, Case.class);

            for (int i = 0; i < caseList.size(); i++) {
                caseList.get(i).setReportingTime(TimeUtil.getTime(caseList.get(i).getReportingTime()));
            }

            int res = caseService.batchInsert(caseList);
            if (res > 0) {
                result.setCode(Result.RESULT_OK);
                result.setMsg("操作成功");
            } else {
                result.setCode(Result.RESULT_ERROR);
                result.setMsg("操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败，主键冲突，请先修改重复警情编号");
            return result;
        }
    }

    /**
     * 获取未分派的案件
     * @param selectConditionJson 查询数据
     * @return 操作结果
     */
    @RequestMapping("/getPendingCases")
    public Result getCases(String selectConditionJson, int page, int limit) {

        SelectCondition selectCondition = JSONObject.parseObject(selectConditionJson, SelectCondition.class);

        String strWhere = "";
        strWhere += "isStartProcess='" + selectCondition.getStartProcess() +"'";
        if (!selectCondition.getCaseId().equals("") && !selectCondition.getCaseId().equals(null)) {
            strWhere += " and caseid = '" + selectCondition.getCaseId()+"'";
        }
        if (!selectCondition.getCaseLevel().equals("") && !selectCondition.getCaseLevel().equals(null)) {
            strWhere += " and reportinglevel = '" + selectCondition.getCaseLevel()+"'";
        }
        if (!selectCondition.getCaseContent().equals("") && !selectCondition.getCaseContent().equals(null)) {
            strWhere += " and reportingcontent like concat('%','" + selectCondition.getCaseContent() + "','%')";
        }
        if (!selectCondition.getStartTime().equals("") && !selectCondition.getStartTime().equals(null)) {
            strWhere += " and reportingtime >='" + selectCondition.getStartTime()+"'";
        }
        if (!selectCondition.getEndTime().equals("") && !selectCondition.getEndTime().equals(null)) {
            strWhere += " and reportingtime <='" + selectCondition.getEndTime()+"'";
        }

        Page pg = new Page("T_Case", "caseid",1 , true, "*", limit, page, strWhere);

        List<Map<String, Object>> maps = pageService.selectByPage(pg);

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(maps);
        List<Case> caseList = jsonArray.toJavaList(Case.class);
        Result result = new Result();
        if (caseList.size() > 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(caseList);
            result.setCount(Long.parseLong(maps.get(0).get("PageCount") + ""));
        } else if (caseList.size() == 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("无数据");
            result.setData(caseList);
            result.setCount(0);
        }else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     *  通过id获取警情信息
     * @param caseId
     * @return
     */
    @RequestMapping("/getCasesById")
    public Result getCasesById(String caseId) {
        Result result = new Result();
        if (caseId == null || "".equals(caseId)) {
            result.setMsg("警情号为空");
            result.setCode(Result.RESULT_ERROR);
            return result;
        }
        Case cases = new Case();
        cases.setCaseId(caseId);
        List<Case> list = caseService.selectCases(cases);
        result.setMsg("空");
        if (!list.isEmpty()) {
            result.setData(list.get(0));
            result.setMsg("查询成功");
        }
        return result;
    }

    @RequestMapping("/deleteByCaseId")
    public Result deleteByCaseId(String caseId, HttpServletRequest request) throws Exception {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        if(vUserInfo.getGrade()==1||vUserInfo.getGrade()==2){
            if(!caseService.delete(caseId)){
                result.setCode(result.RESULT_ERROR);
                result.setMsg("删除失败");
            }
        }else{
           result.setMsg("抱歉,您无法进行警情删除");
        }
        return result;
    }

}
